#include <iostream>
#include <string>
#include <bitset>

using namespace std;

// 2311. 小于等于 K 的最长二进制子序列
// https://leetcode.cn/problems/longest-binary-subsequence-less-than-or-equal-to-k/description/?envType=daily-question&envId=2025-06-26

class Solution
{
public:
    int longestSubsequence(string s, int k)
    {
        int binCnt = 0;
        int temp = k;
        while (temp != 0)
        {
            temp >>= 1;
            binCnt++;
        }

        int n = s.size();
        if (n <= (binCnt - 1))
        {
            return n;
        }

        string end = s.substr(n - binCnt);
        int range = binCnt - 1;
        if (bitset<32>(end).to_ullong() <= k)
        {
            range = binCnt;
        }
        int ans = 0;
        for (int i = 0; i < n - range; i++)
        {
            if (s[i] == '0')
            {
                ans++;
            }
        }
        return ans + range;
    }
};

int main()
{
    return 0;
}